package com.authine.cloudpivot.ext.service.impl;

import com.alibaba.fastjson.JSON;
import com.authine.cloudpivot.engine.api.facade.BizObjectFacade;
import com.authine.cloudpivot.engine.api.model.runtime.AttachmentModel;
import com.authine.cloudpivot.engine.service.utils.sftp.CustSFTP;
import com.authine.cloudpivot.ext.service.AttachmentService;
import com.authine.cloudpivot.ext.service.SshService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;

/**
 * @Author hxd
 * @Date 2022/4/27 9:50
 * @Description
 **/
@Service
@Slf4j
public class AttachmentServiceImpl implements AttachmentService {
    @Autowired
    SshService sshService;
    @Autowired
    BizObjectFacade bizObjectFacade;
    @Autowired
    private RedisTemplate<String, AttachmentModel> redisTemplate;

    @Override
    public void attachmentCopy(Map<String,Object> data, List<String> codeList)  {

        if (CollectionUtils.isEmpty(codeList)) {
            return;
        }
        log.info("需要拷贝附件的字段:{}", StringUtils.join(codeList,","));

        Map<String, List<Map<String, Object>>> m = new HashMap<>();

        codeList.forEach(code -> {
            List<Map<String, Object>> list = (List<Map<String, Object>>) data.get(code);
            if (CollectionUtils.isNotEmpty(list)) {
                m.put(code, list);
            }
        });

        if (m.isEmpty()) {
            return;
        }
        Map<String, String> copyFiles = new HashMap<>();

        BiConsumer tConsumer = (k, v) -> copyFiles.put((String) k,(String) v);

        Map<String, Object> fileMap = new HashMap<>();
        m.forEach((code,v)->{
            List<Map<String, Object>> newFileList = v.stream().map(file -> attachmentObj(file, tConsumer)).collect(Collectors.toList());
            fileMap.put(code,newFileList);
        });
       log.info("需要拷贝的附件数量:{}",copyFiles.size());
        sshService.copyFiles(copyFiles);
        data.putAll(fileMap);
    }




    public static final String KEY_WORKFLOW_ATTACHMENT = "cloudpivot:fileupload:workflow_attachment";
    public Map<String,Object> attachmentObj(Map<String,Object> file, BiConsumer function) {

        String sourceRefId = (String) file.get("refId");
        String name = (String) file.get("name");

        String sourceFile = sourceRefId.concat(name);
        String refId = UUID.randomUUID().toString().replaceAll("-","");
        String targetFile = refId.concat(name);

        function.accept(sourceFile,targetFile);

        String fileExtension = (String) file.get("fileExtension");
        Integer fileSize = (Integer) file.get("fileSize");
        String mimeType = (String) file.get("mimeType");


        AttachmentModel resourceModel = new AttachmentModel();
        resourceModel.setRefId(refId);
        resourceModel.setName(name);
        resourceModel.setFileExtension(fileExtension);
        resourceModel.setFileSize(fileSize);
        resourceModel.setMimeType(mimeType);
        redisTemplate.boundHashOps(KEY_WORKFLOW_ATTACHMENT).putIfAbsent(refId, resourceModel);
        redisTemplate.expire(KEY_WORKFLOW_ATTACHMENT, 30, TimeUnit.SECONDS);



        return (Map<String, Object>) JSON.parse(JSON.toJSONString(resourceModel));

    }



}
